using System.Collections.Generic;
using DslCrossModelFramework.ModelTraversal;
using Microsoft.VisualStudio.Modeling;

namespace DslCrossModelFramework.ModelRelationship {
    /// <summary>
    /// Represents a collection of filters
    /// </summary>
    public class ModelElementFilterCollection : List<IModelElementFilter>, IModelElementFilter {
        /// <summary>
        /// Filters the collection.
        /// </summary>
        /// <param name="collectionToFilter">The collection to filter.</param>
        /// <returns></returns>
        public IList<ModelElement> FilterCollection(IEnumerable<ModelElement> collectionToFilter) {
            IList<ModelElement> list = new List<ModelElement>(collectionToFilter);
            foreach (IModelElementFilter filter in this) {
                list = filter.FilterCollection(list);
            }
            return list;
        }

        /// <summary>
        /// Determines whether the specified model element is valid for this filter.
        /// </summary>
        /// <param name="modelElement">The model element.</param>
        /// <returns>
        /// 	<c>true</c> if the specified model element is valid for this filter; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValid(ModelElement modelElement) {
            foreach (IModelElementFilter filter in this) {
                if(!filter.IsValid(modelElement)) {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Determines whether the specified path segment is valid.
        /// </summary>
        /// <param name="pathSegment">The path segment.</param>
        /// <returns>
        /// 	<c>true</c> if the specified model segment is valid for this filter; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValid(LinkedListNode<IModelPathSegment> pathSegment) {
            foreach (IModelElementFilter filter in this) {
                if (!filter.IsValid(pathSegment)) {
                    return false;
                }
            }
            return true;
        }
    }
}